<?php
/**
 * ClassName: archives
 * Description:
 * @Author 周昌
 * @Create 2023/11/27 13:45
 */

namespace app;


use epii\server\Args;
use libs\Tool;
use model\ArchivesModel;
use model\MemberModel;
use service\admin\HospitalService;
use service\admin\ArchivesService;
use service\admin\MemberService;
use service\admin\ReservationService;

class archives extends base
{
    public function init()
    {
        parent::init();
        $this->accessDenied();
    }

    public function index()
    {
        if($this->adminInfo['role']==1){
            $this->assign('hospitals',HospitalService::getSelect());
        }
        //筛选
        $this->_as_sex = MemberService::memberSexSelect();
        $this->adminUiDisplay();
    }

    public function ajax_data()
    {
        $map = [];
        //登录用户权限判断
        $hospital_group_id=$this->adminInfo['hospital_group_id'];
        if($hospital_group_id){
            $map[]=['hospital_group_id','=',$hospital_group_id];
        }
        $name = Args::params('name');
        if ($name) {
            $map[] = ['name', "LIKE", '%' . $name . '%'];
        }
        //家庭组手机号
        $phone=Args::params('phone');
        if($phone){
            $member_id=MemberModel::getValue(['phone'=>$phone],'id');
            if($member_id){
                $map[]=['member_id','=',$member_id];
            }
        }
        $sex=Args::params('sex/d');
        if(is_int($sex)){
            $map[]=['sex','=',$sex];
        }
        $id_card=Args::params('id_card');
        if($id_card){
            $map[]=['id_card','=',$id_card];
        }


        echo ArchivesService::getAjaxData($map);
    }

    public function add_edit()
    {
        $id = Args::params('id');
        if ($_SERVER['REQUEST_METHOD'] == "POST") {
            $data = Args::params('r');
            //登录权限判断
            $hospital_id=$this->adminInfo['hospital_id'];
            if(!empty($hospital_id)){
                $data['hospital_id']=$hospital_id;
            }
            //验证
            $this->validate($data,[
                'phone|手机号'=>'require|mobile',
                'name|姓名'=>'require|chsAlphaNum',
                'sex|性别'=>'require|integer',
                'relation|关系'=>'require|integer',
                'family_group|归属家庭组'=>'require|max:20',
                'birth_date|阳历生日'=>'max:20',
                'lunar_birth|阴历生日'=>'max:20',
                'nation|民族'=>'chs',
                'height|身高'=>'float|>:0',
                'weight|体重'=>'float|>:0',
                'address|家庭地址'=>'max:30',
                'education|学历'=>'integer',
                'work|工作性质'=>'max:50',
                'family_rank|家庭排行'=>'max:10',
                'marriage_childbirth|婚育情况'=>'max:30',
                'hospital_id|所属医院'=>'require|integer|>=:0',
            ]);
            //判断用户是否已经存在
            $res=MemberService::memberIsExist($id,$data['phone']);
            if($res['code']){
                $this->error('手机号尚未注册请先去注册用户');
            }
            unset($data['family_group']);
            unset($data['phone']);
            $data['member_id']=$res['data']['id'];
            if ($id) {
                $res = ArchivesService::edit($id,$data);
            } else {
                $data['addtime']=time();
                $res=ArchivesService::add($data);
            }
            $res['code']?$this->success($res['msg']):$this->error($res['msg']);
        }
        $this->_as_info = $id ? ArchivesService::getArchivesInfo($id) : [];
        $this->_as_id = $id;
        $this->_as_relation = ArchivesService::archivesRelationSelect();
        $this->_as_sex = MemberService::memberSexSelect();
        $this->_as_education = ArchivesService::archivesEducationSelect();
        $this->_as_hospitals = HospitalService::getSelect();

        $this->adminUiDisplay();
    }

    /**
     * @return array|false|string* 删除
     */
    public function del()
    {
        $id = Args::params('id');
        //todo 判断用户下是否已有会员卡，无则可以删除
        $res = ArchivesService::del($id);
        if ($res['code']) {
            $this->success($res['msg'],'refresh');
        } else {
            $this->error($res['msg']);
        }
    }
    //获取家庭组
    public function getFamily()
    {
        $phone=Args::params('phone/1','手机号不能为空');
        $res=ArchivesService::getFamilyGroup(['phone'=>$phone]);
        exit(json_encode($res));
    }
    // 问诊下 患者基础信息编辑
    public function archivesEdit()
    {
        $id=Args::params('id/d');
        $field=Args::params('field');//编辑的字段名
        $field_value=Args::params('field_value');//修改后的值
        $res=ArchivesService::edit($id,[$field=>$field_value]);
        $res['code']?$this->jsonSuccess($res['msg']):$this->jsonError($res['msg']);
    }
}